(() => {

    'use strict';
    goog.provide('Blockly.Blocks.ruilong');
    goog.require('Blockly.Blocks');
    goog.require('path');

    const mediaDirPath = path.join(document.currentScript.src, '../../media/');

    Blockly.Msg['RUILONG_HUE'] = 120;
  var L_PIN = [["A0", "A0"], ["A1", "A1"], ["A2", "A2"], ["A6", "A6"], ["A7", "A7"]];
    var S_PIN = [["A3", "A3"]];
    var B_PIN = [["D2", "2"], ["D3", "3"]];
    var BU_PIN = [["D8", "8"]];
    var LED_PIN = [["D9", "9"]];
    var U1_PIN = [["D10", "10"]];
    var U2_PIN = [["D11", "11"]];
    var IR_PIN = [["D12", "12"]];
    var BL_PIN = [["D13", "13"]];

    var CH_PIN = [["左轮电机D4-D5", "1"], ["右轮电机D6-D7", "2"]];

    var CH8_PIN = [["M0", "0"], ["M1", "1"], ["M2", "2"], ["M3", "3"], ["M4", "4"], ["M5", "5"], ["M6", "6"], ["M7", "7"]];

    //机器人部分程序
    Blockly.Blocks.ruilong_robot_linefinder_pins_analog = {
        init: function () {
            this.setColour(Blockly.Msg['RUILONG_HUE']);
            this.appendDummyInput("")
                .appendField(new Blockly.FieldDropdown(L_PIN), 'PIN');
            this.setOutput(true, Number);
        }
    };

    Blockly.Blocks.ruilong_robot_sound_pins_analog = {
        init: function () {
            this.setColour(Blockly.Msg['RUILONG_HUE']);
            this.appendDummyInput("")
                .appendField(new Blockly.FieldDropdown(S_PIN), 'PIN');
            this.setOutput(true, Number);
        }
    };

    Blockly.Blocks.ruilong_robot_button_pins_digital = {
        init: function () {
            this.setColour(Blockly.Msg['RUILONG_HUE']);
            this.appendDummyInput("")
                .appendField(new Blockly.FieldDropdown(B_PIN), 'PIN');
            this.setOutput(true, Number);
        }
    };

    Blockly.Blocks.ruilong_robot_hc_sr04_trig = {
        init: function () {
            this.setColour(Blockly.Msg['RUILONG_HUE']);
            this.appendDummyInput("")
                .appendField(new Blockly.FieldDropdown(U1_PIN), 'PIN1');
            this.setOutput(true, Number);
        }
    };

    Blockly.Blocks.ruilong_robot_hc_sr04_echo = {
        init: function () {
            this.setColour(Blockly.Msg['RUILONG_HUE']);
            this.appendDummyInput("")
                .appendField(new Blockly.FieldDropdown(U2_PIN), 'PIN2');
            this.setOutput(true, Number);
        }
    };

    Blockly.Blocks.ruilong_robot_pins_ir_recv = {
        init: function () {
            this.setColour(Blockly.Msg['RUILONG_HUE']);
            this.appendDummyInput("")
                .appendField(new Blockly.FieldDropdown(IR_PIN), 'PIN');
            this.setOutput(true, Number);
        }
    };

    Blockly.Blocks.ruilong_robot_pins_blink = {
        init: function () {
            this.setColour(Blockly.Msg['RUILONG_HUE']);
            this.appendDummyInput("")
                .appendField(new Blockly.FieldDropdown(BL_PIN), 'PIN');
            this.setOutput(true, Number);
        }
    };


    Blockly.Blocks.ruilong_robot_pins_8833motor = {
        init: function () {
            this.setColour(Blockly.Msg['RUILONG_HUE']);
            this.appendDummyInput("")
                .appendField(new Blockly.FieldDropdown(CH_PIN), 'ID')
            this.setOutput(true, Number);
        }
    };


    Blockly.Blocks.ruilong_robot_pins_I2Cmotor = {
        init: function () {
            this.setColour(Blockly.Msg['RUILONG_HUE']);
            this.appendDummyInput("")
                .appendField(new Blockly.FieldDropdown(CH8_PIN), 'ID')
            this.setOutput(true, Number);
        }
    };

    Blockly.Blocks.ruilong_robot_pins_buzzer = {
        init: function () {
            this.setColour(Blockly.Msg['RUILONG_HUE']);
            this.appendDummyInput("")
                .appendField(new Blockly.FieldDropdown(BU_PIN), 'PIN')
            this.setOutput(true, Number);
        }
    };
    Blockly.Blocks.ruilong_robot_pins_led = {
        init: function () {
            this.setColour(Blockly.Msg['RUILONG_HUE']);
            this.appendDummyInput("")
                .appendField(new Blockly.FieldDropdown(LED_PIN), 'PIN')
            this.setOutput(true, Number);
        }
    };

    Blockly.Blocks.ruilong_robot_inout_onoff = {
        init: function () {
            this.setColour(Blockly.Msg['RUILONG_HUE']);
            this.appendDummyInput("")
                .appendField(new Blockly.FieldDropdown([["开", "1"], ["关", "0"]]), 'BL')
            this.setOutput(true, Number);
        }
    };


    Blockly.Blocks.ruilong_robot_linefinder = {
        init: function () {
            this.setColour(Blockly.Msg['RUILONG_HUE']);
            this.appendDummyInput("")
                .appendField("寻迹传感器");
            this.appendValueInput("PIN", Number)
                .appendField(Blockly.Msg.MIXLY_PIN)
                .setCheck(Number);
            this.setInputsInline(true);
            this.setOutput(true, Number);
            this.setTooltip('');
        }
    };

    Blockly.Blocks.ruilong_robot_sound = {
        init: function () {
            this.setColour(Blockly.Msg['RUILONG_HUE']);
            this.appendDummyInput("")
                .appendField("声音传感器");
            this.appendValueInput("PIN", Number)
                .appendField(Blockly.Msg.MIXLY_PIN)
                .setCheck(Number);
            this.setInputsInline(true);
            this.setOutput(true, Number);
            this.setTooltip('');
        }
    };
    Blockly.Blocks.ruilong_robot_tm1650_power = {
        init: function () {
            this.setColour(Blockly.Msg['RUILONG_HUE']);
            this.appendDummyInput()
                .appendField("TM1650四位数码管")
                .appendField(new Blockly.FieldDropdown([[Blockly.Msg.MIXLY_LCD_STAT_ON, "displayOn"], [Blockly.Msg.MIXLY_LCD_STAT_OFF, "displayOff"], [Blockly.Msg.MIXLY_LCD_STAT_CLEAR, "clear"]]), "STAT");
            this.setInputsInline(true);
            this.setPreviousStatement(true, null);
            this.setNextStatement(true, null);
        }
    };

    Blockly.Blocks.ruilong_robot_tm1650_displayString = {
        init: function () {
            this.setColour(Blockly.Msg['RUILONG_HUE']);
            this.appendDummyInput()
                .appendField("TM1650四位数码管")
            this.appendValueInput("VALUE")
                .setAlign(Blockly.ALIGN_RIGHT)
                .appendField("显示数值");
            this.setInputsInline(true);
            this.setPreviousStatement(true, null);
            this.setNextStatement(true, null);
        }
    };

    Blockly.Blocks.ruilong_robot_tm1650_showDot = {
        init: function () {
            this.setColour(Blockly.Msg['RUILONG_HUE']);
            this.appendDummyInput()
                .appendField("TM1650四位数码管")
                .appendField("第")
                .appendField(new Blockly.FieldDropdown([["1", "0"], ["2", "1"], ["3", "2"], ["4", "3"]]), "NO")
                .appendField("个")
                .appendField("小数点")
                .appendField(new Blockly.FieldDropdown([[Blockly.Msg.MIXLY_4DIGITDISPLAY_ON, "true"], [Blockly.Msg.MIXLY_4DIGITDISPLAY_OFF, "false"]]), "STAT");
            this.setInputsInline(true);
            this.setPreviousStatement(true, null);
            this.setNextStatement(true, null);
        }
    };
    Blockly.Blocks.ruilong_robot_button = {
        init: function () {
            this.setColour(Blockly.Msg['RUILONG_HUE']);
            this.appendDummyInput("")
                .appendField("按钮传感器")
            this.appendValueInput("PIN", Number)
                .appendField(Blockly.Msg.MIXLY_PIN)
                .setCheck(Number);
            this.setOutput(true, Boolean);
            this.setInputsInline(true);
            this.setTooltip('');
        }
    };

    //执行器-电机转速
    Blockly.Blocks.ruilong_robot_8833motor = {
        init: function () {
            this.setColour(Blockly.Msg['RUILONG_HUE']);
            this.appendValueInput("ID").setCheck(Number).appendField("8833驱动器电机控制");
            this.appendValueInput("SPEED").setCheck(Number).appendField("速度-255~255");
            this.setInputsInline(true);
            this.setPreviousStatement(true);
            this.setNextStatement(true);
        }
    };


    //执行器-电机转速
    Blockly.Blocks.ruilong_robot_8833motor2 = {
        init: function () {
            this.setColour(Blockly.Msg['RUILONG_HUE']);
            this.appendDummyInput("").appendField("双电机控制");
            this.appendValueInput("SPEED").setCheck(Number).appendField("速度-255~255");
            this.setInputsInline(true);
            this.setPreviousStatement(true);
            this.setNextStatement(true);
        }
    };
    //蜂鸣器频率
    var TONE_NOTES = [
        ["NOTE_C5", "532"],
        ["NOTE_D5", "587"],
        ["NOTE_E5", "659"],
        ["NOTE_F5", "698"],
        ["NOTE_G5", "784"],
        ["NOTE_A5", "880"],
        ["NOTE_B5", "988"],
        ["NOTE_C6", "1048"],
        ["NOTE_D6", "1176"],
        ["NOTE_E6", "1320"],
        ["NOTE_F6", "1396"],
        ["NOTE_G6", "1568"],
        ["NOTE_A6", "1760"],
        ["NOTE_B6", "1976"],
        ["NOTE_C7", "2096"],
        ["NOTE_D7", "2352"],
        ["NOTE_E7", "2640"],
        ["NOTE_F7", "2792"],
        ["NOTE_G7", "3136"],
        ["NOTE_A7", "3520"],
        ["NOTE_B7", "3952"]];

    //执行器-蜂鸣器的频率选择
    Blockly.Blocks.ruilong_robot_tone_notes = {
        init: function () {
            this.setColour(Blockly.Msg['RUILONG_HUE']);
            this.appendDummyInput("").appendField(new Blockly.FieldDropdown(TONE_NOTES), 'STAT');
            this.setOutput(true, Number);
        }
    };

    //执行器-蜂鸣器模块
    Blockly.Blocks.ruilong_robot_tone = {
        init: function () {
            this.setColour(Blockly.Msg['RUILONG_HUE']);
            this.appendDummyInput("").appendField("蜂鸣器模块");
            this.appendValueInput("PIN", Number).appendField(Blockly.Msg.MIXLY_PIN).setCheck(Number);
            this.appendValueInput('FREQUENCY').setCheck(Number).appendField(Blockly.Msg.MIXLY_FREQUENCY);
            this.appendValueInput('DURATION', Number).setCheck(Number).appendField(Blockly.Msg.MIXLY_DURATION).appendField("(ms)");
            this.setInputsInline(true);
            this.setPreviousStatement(true);
            this.setNextStatement(true);
        }
    };

    //执行器-蜂鸣器停止模块
    Blockly.Blocks.ruilong_robot_newNoTone = {
        init: function () {
            this.setColour(Blockly.Msg['RUILONG_HUE']);
            this.appendDummyInput("").appendField("蜂鸣器停止模块");
            this.appendValueInput("PIN", Number).appendField(Blockly.Msg.MIXLY_PIN).setCheck(Number);
            this.appendValueInput("DELAY_TIME", Number).appendField(Blockly.Msg.MIXLY_DELAY).setCheck(Number).appendField("(ms)");
            this.setInputsInline(true);
            this.setPreviousStatement(true);
            this.setNextStatement(true);
        }
    };

    //RGB
    Blockly.Blocks.ruilong_robot_rgb_init = {
        init: function () {
            this.setColour(Blockly.Msg['RUILONG_HUE']);
            this.appendDummyInput("")
                .appendField(Blockly.Msg.MIXLY_RGB)
            this.appendValueInput("PIN", Number)
                .setCheck(Number)
                .setAlign(Blockly.ALIGN_RIGHT)
                .appendField(Blockly.Msg.MIXLY_PIN);
            this.appendValueInput("LEDCOUNT")
                .setCheck(Number)
                .setAlign(Blockly.ALIGN_RIGHT)
                .appendField(Blockly.Msg.MIXLY_RGB_COUNT);
            this.setInputsInline(true);
            this.setPreviousStatement(true, null);
            this.setNextStatement(true, null);
            this.setTooltip('');
        }
    };

    Blockly.Blocks.ruilong_robot_rgb = {
        init: function () {
            this.setColour(Blockly.Msg['RUILONG_HUE']);
            this.appendDummyInput("")
                .appendField(Blockly.Msg.MIXLY_RGB)
            this.appendValueInput("PIN", Number)
                .setCheck(Number)
                .setAlign(Blockly.ALIGN_RIGHT)
                .appendField(Blockly.Msg.MIXLY_PIN);
            this.appendValueInput("_LED_")
                .setCheck(Number)
                .setAlign(Blockly.ALIGN_RIGHT)
                .appendField(Blockly.Msg.MIXLY_RGB_NUM);
            this.appendValueInput("RVALUE")
                .setCheck(Number)
                .setAlign(Blockly.ALIGN_RIGHT)
                .appendField(Blockly.Msg.MIXLY_RGB_R);
            this.appendValueInput("GVALUE")
                .setCheck(Number)
                .setAlign(Blockly.ALIGN_RIGHT)
                .appendField(Blockly.Msg.MIXLY_RGB_G);
            this.appendValueInput("BVALUE")
                .setCheck(Number)
                .setAlign(Blockly.ALIGN_RIGHT)
                .appendField(Blockly.Msg.MIXLY_RGB_B);
            this.setInputsInline(true);
            this.setPreviousStatement(true, null);
            this.setNextStatement(true, null);
            this.setTooltip('');
        }
    };

    Blockly.Blocks.ruilong_robot_rgb_brightness = {
        init: function () {
            this.setColour(Blockly.Msg['RUILONG_HUE']);
            this.appendDummyInput("")
                .appendField(Blockly.Msg.MIXLY_RGB)
            this.appendValueInput("PIN", Number)
                .setCheck(Number)
                .setAlign(Blockly.ALIGN_RIGHT)
                .appendField(Blockly.Msg.MIXLY_PIN);
            this.appendValueInput("NUM")
                .setCheck(Number)
                .setAlign(Blockly.ALIGN_RIGHT)
                .appendField("亮度");
            this.setInputsInline(true);
            this.setPreviousStatement(true, null);
            this.setNextStatement(true, null);
            this.setTooltip('');
        }
    };

    Blockly.Blocks.ruilong_robot_rgb2 = {
        init: function () {
            this.setColour(Blockly.Msg['RUILONG_HUE']);
            this.appendDummyInput("")
                .appendField(Blockly.Msg.MIXLY_RGB)
            this.appendValueInput("PIN", Number)
                .setCheck(Number)
                .setAlign(Blockly.ALIGN_RIGHT)
                .appendField(Blockly.Msg.MIXLY_PIN);
            this.appendValueInput("_LED_")
                .setCheck(Number)
                .setAlign(Blockly.ALIGN_RIGHT)
                .appendField(Blockly.Msg.MIXLY_RGB_NUM);
            this.appendDummyInput("")
                .setAlign(Blockly.ALIGN_RIGHT)
                .appendField(new Blockly.FieldColour("#ff0000"), "RGB_LED_COLOR");
            this.setInputsInline(true);
            this.setPreviousStatement(true, null);
            this.setNextStatement(true, null);
        }
    };


    Blockly.Blocks.ruilong_robot_rgb3 = {
        init: function () {
            this.setColour(Blockly.Msg['RUILONG_HUE']);
            this.appendDummyInput("")
                .appendField(Blockly.Msg.MIXLY_RGB)
            this.appendValueInput("PIN", Number)
                .setCheck(Number)
                .setAlign(Blockly.ALIGN_RIGHT)
                .appendField(Blockly.Msg.MIXLY_PIN);
            this.appendValueInput("NUM")
                .setCheck(Number)
                .appendField("初始亮度");
            this.appendValueInput("WAIT")
                .setCheck(Number)
                .setAlign(Blockly.ALIGN_RIGHT)
                .appendField("七彩变换切换时间");

            this.setInputsInline(true);
            this.setPreviousStatement(true, null);
            this.setNextStatement(true, null);
        }
    };

    Blockly.Blocks.ruilong_robot_rgb4 = {
        init: function () {
            this.setColour(Blockly.Msg['RUILONG_HUE']);
            this.appendDummyInput("")
                .appendField(Blockly.Msg.MIXLY_RGB)
            this.appendValueInput("PIN", Number)
                .setCheck(Number)
                .setAlign(Blockly.ALIGN_RIGHT)
                .appendField(Blockly.Msg.MIXLY_PIN);
            this.appendValueInput("NUM")
                .setCheck(Number)
                .appendField("初始亮度");
            this.appendValueInput("WAIT")
                .setCheck(Number)
                .setAlign(Blockly.ALIGN_RIGHT)
                .appendField("七彩循环切换时间");

            this.setInputsInline(true);
            this.setPreviousStatement(true, null);
            this.setNextStatement(true, null);
        }
    };


    //超声测距
    Blockly.Blocks.ruilong_robot_hc_sr04 = {
        init: function () {
            this.setColour(Blockly.Msg['RUILONG_HUE']);
            this.appendDummyInput("")
                .appendField("超声波测距")
            //.appendField(new Blockly.FieldImage(mediaDirPath + "/lobo/RGB_BLOCK.jpg", 33, 32))
            this.appendValueInput("PIN1", Number)
                .setCheck(Number)
                .setAlign(Blockly.ALIGN_RIGHT)
                .appendField("Trig#")
            this.appendValueInput("PIN2", Number)
                .setCheck(Number)
                .setAlign(Blockly.ALIGN_RIGHT)
                .appendField("Echo#");

            this.setInputsInline(true);
            this.setOutput(true, Number);
            this.setTooltip('');
        }
    };
    //红外接收模块
    Blockly.Blocks.ruilong_robot_ir_recv = {
        init: function () {
            this.setColour(Blockly.Msg['RUILONG_HUE']);
            this.appendValueInput("PIN", Number)
                .appendField(new Blockly.FieldTextInput('ir_item'), 'VAR')
                .appendField(Blockly.Msg.MIXLY_IR_RECEIVE)
                .setCheck(Number);
            this.appendStatementInput('DO')
                .appendField(Blockly.Msg.MIXLY_IR_RECEIVE_YES);
            this.appendStatementInput('DO2')
                .appendField(Blockly.Msg.MIXLY_IR_RECEIVE_NO);
            this.setPreviousStatement(true);
            this.setNextStatement(true);
            this.setInputsInline(true);
            this.setTooltip(Blockly.Msg.MIXLY_IR_RECIEVE_TOOLTIP);
        },
        getVars: function () {
            return [this.getFieldValue('VAR')];
        },
        renameVar: function (oldName, newName) {
            if (Blockly.Names.equals(oldName, this.getFieldValue('VAR'))) {
                this.setTitleValue(newName, 'VAR');
            }
        }
    };

    //蓝色闪灯
    Blockly.Blocks.ruilong_robot_blink = {
        init: function () {
            this.setColour(Blockly.Msg['RUILONG_HUE']);
            this.appendValueInput("PIN").setCheck(Number).appendField("蓝色闪灯");
            this.appendValueInput("BL").setCheck([Number, Boolean]).appendField("设为");
            this.setInputsInline(true);
            this.setPreviousStatement(true);
            this.setNextStatement(true);
        }
    };

    Blockly.Blocks.ruilong_robot_blink_read = {
        init: function () {
            this.setColour(Blockly.Msg['RUILONG_HUE']);
            this.appendDummyInput("")
                .appendField("蓝色闪灯状态")
            this.appendValueInput("PIN", Number)
                .appendField(Blockly.Msg.MIXLY_PIN)
                .setCheck(Number);
            this.setOutput(true, Boolean);
            this.setInputsInline(true);
            this.setTooltip('');
        }
    };

    })();